package com.wanghengzhi.kata;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

/**
 * @author 王潇
 * @since 2021/11/22 10:44
 */
public class Anagram {
    public Set<String> generate(String word) {
        if (word.length() == 1) {
            return new HashSet<>(Collections.singleton(word));
        }
        Set<String> result = new HashSet<>();
        for (int index = 0; index < word.length(); index++) {
            char firstChar = word.charAt(index);
            String surplus = deleteCharAt(word, index);
            Set<String> combinations = generate(surplus);
            for (String combination : combinations) {
                result.add(firstChar + combination);
            }
        }
        return result;
    }


    private String deleteCharAt(String word, int index) {
        return new StringBuilder(word).deleteCharAt(index).toString();
    }

}
